﻿
// MFCApplication1Dlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "MFCApplication1.h"
#include "MFCApplication1Dlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCApplication1Dlg 对话框

BOOL CALLBACK G_Com_RecvFinished(void * p_vpOwner, BYTE * p_bypRecvBuff, DWORD p_dwRecvLen)
{
	CMFCApplication1Dlg * l_pDlg = (CMFCApplication1Dlg*)p_vpOwner;

	l_pDlg->RecvFinished(p_bypRecvBuff, p_dwRecvLen);

	return TRUE;
}

CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_MFCAPPLICATION1_DIALOG, pParent)
    , m_strSendData(_T(""))
    , m_strPort(_T(""))
    , m_strRecv(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT1, m_strSendData);
    DDX_Text(pDX, IDC_EDIT2, m_strPort);
    DDX_Text(pDX, IDC_EDIT3, m_strRecv);
}

BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication1Dlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CMFCApplication1Dlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CMFCApplication1Dlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CMFCApplication1Dlg::OnBnClickedButton4)
END_MESSAGE_MAP()


// CMFCApplication1Dlg 消息处理程序

BOOL CMFCApplication1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	T_COM_INFO l_tcom;
	l_tcom.iPortName = 3;

	m_strPort = L"COM3";
	m_strSendData = L"send";

	T_COM_CHARSFLAG l_tcomFlags;
	l_tcomFlags.bDealSendBegin = 1;
	l_tcomFlags.cpSendBegin = "@S开始";
	l_tcomFlags.bDealRecvBegin = 0;
	l_tcomFlags.cpRecvBegin = "@R开始";
	l_tcomFlags.bDealSendEnd = 1;
	l_tcomFlags.cpSendEnd = "@S结束";
	l_tcomFlags.bDealRecvEnd = 0;
	l_tcomFlags.cpRecvEnd = "@R结束";

    if (a.Open(l_tcom, 0))
    {
		a.SetCharsFlag(l_tcomFlags);
        UpdateData(FALSE);
    }

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMFCApplication1Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFCApplication1Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CMFCApplication1Dlg::OnBnClickedButton1()
{
    UpdateData(TRUE);

    // TODO: 在此添加控件通知处理程序代码
    //const char * port = m_strPort.GetBuffer(m_strPort.GetLength());
    //const char * send = m_strSendData.GetBuffer(m_strSendData.GetLength());
    char send[256] = { 0 };
    strcpy_s(send, (_bstr_t)m_strSendData);
    char recv[256];
    memset(recv, 0, sizeof(recv));
    int recvLen = 0;

	//int l_iInBuffSize = 0, l_iOutBuffSize = 0;
	//a.GetBufferSize(l_iInBuffSize, l_iOutBuffSize);
	//a.SetBufferSize(0,0);

	{
		int l_iLen = m_strSendData.GetLength();
		a.SendData((BYTE*)send, l_iLen, 1);

        if (a.RecvData((BYTE*)recv, (DWORD&)recvLen, 3000))
        {
			//m_strRecv = recv;
			m_strRecv = L"优先级success";
			strcpy_s(send, (_bstr_t)m_strRecv);
			l_iLen = recvLen;
			a.SendData((BYTE*)send, 7, 1);

			//a.SuspendReadThread(); // 与读线程共享接收缓冲区
		}
		else
		{
			m_strRecv = L"优先级fail";
			strcpy_s(send, (_bstr_t)m_strRecv);
			l_iLen = m_strRecv.GetLength();
			a.SendData((BYTE*)send, 4, 0);
		}

		UpdateData(FALSE);

		//Sleep(3000);
    }
}

BOOL CMFCApplication1Dlg::RecvFinished(BYTE * p_bypRecvBuff, DWORD p_dwRecvLen)
{
	UpdateData(TRUE);

	char recv[256];
	memset(recv, 0, sizeof(recv));
	memcpy(recv, reinterpret_cast<char*>(p_bypRecvBuff), p_dwRecvLen);
	a.SendData((BYTE*)recv, p_dwRecvLen, 1);

	USES_CONVERSION;
	m_strRecv = A2T(recv);
	
	UpdateData(FALSE);

	return TRUE;
}


void CMFCApplication1Dlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	char send[256] = { 0 };
	strcpy_s(send, (_bstr_t)m_strSendData);
	int l_iLen = m_strSendData.GetLength();
	a.SendData((BYTE*)send, l_iLen, 1);

	a.InitReadThread(this, G_Com_RecvFinished);
	a.ResumeReadThread();
}


void CMFCApplication1Dlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	a.SuspendReadThread();
}


void CMFCApplication1Dlg::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码
	a.ExitReadThread();

	char send[256] = { 0 };
	m_strSendData = L"手动读";
	strcpy_s(send, (_bstr_t)m_strSendData);
	int l_iLen = m_strSendData.GetLength();
	a.SendData((BYTE*)send, l_iLen, 1);
}
